import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

import javax.swing.JFrame;


public class Game extends Canvas implements Runnable{
	private static final long serialVersionUID = 1L;
	private final static int SCALE = 3;
	private final static int WIDTH = 350*SCALE;
	private final static int HEIGHT = 220*SCALE;
	final Dimension SIZE = new Dimension(WIDTH,HEIGHT);
	String title = "PONG";
	boolean running = false;
	@SuppressWarnings("unused")
	private int tickCount = 0;
	static Screen gScreen = new Screen();
	static final int LEFT = 1;
	static final int RIGHT = 2;
	static int winner = 0;
	
	public static void main(String[] args){
		new Game().start();
	}
	
	public synchronized void start() {
		running = true;
		new Thread(this).start();
		
	}
	
	public synchronized void stop() {
		running = false;
		
	}
	
	private BufferedImage image = new BufferedImage(WIDTH,HEIGHT, BufferedImage.TYPE_INT_RGB);
	@SuppressWarnings("unused")
	private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
	
	public Game(){
		JFrame frame = new JFrame(title);
		frame.setSize(SIZE);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		frame.add(this,BorderLayout.CENTER);
		this.addMouseMotionListener(new MListener());
		this.addKeyListener(new KL());
		frame.setResizable(false);
		frame.setVisible(true);
		this.requestFocus();
	}
	@Override
	public void run() {
		long lastTime = System.nanoTime();
		double nsPerTick = 1000000000D/60D;
		
		int ticks =0;
		int frames = 0;
		long lastTimer = System.currentTimeMillis();
		double delta = 0;
		while(running){
			long now = System.nanoTime();
			delta += (now - lastTime)/nsPerTick;
			lastTime = now;
			boolean shouldRender = true;
			
			
			
			while (delta >= 1){
				ticks++;
				tick();
				delta-=1;
				shouldRender = true;
			}

			if (shouldRender){
			frames++;
			render();
			}
			if (System.currentTimeMillis() - lastTimer >= 1000){
				lastTimer += 1000;
				//System.out.println(ticks +" ticks, " + frames + " frames");
				ticks = 0;
				frames = 0;
			}
		}
		
	}
	/*
		final int LEFT = 1;
		final int RIGHT = 2;
		int winner = 0;
	 */

	private void tick(){
		tickCount++;
		Screen.update();
		if(winner != 0){
			this.addKeyListener(new VLis());
		}
	}
	
	private void render(){
		BufferStrategy bs = getBufferStrategy();
		if (bs == null){
			createBufferStrategy(4);
			return;
		}
		
		Graphics g = bs.getDrawGraphics();
		g.drawImage(image, 0, 0, getWIDTH(), getHEIGHT(), null);
		gScreen.paintComponent(g);
		g.dispose();
		bs.show();
	}

	public static int getWIDTH() {
		return WIDTH;
	}

	public static int getHEIGHT() {
		return HEIGHT;
	}

	private static Font font = new Font("Cambria", Font.PLAIN, 123);
	

}

class MListener implements MouseMotionListener{

	boolean want =false; 
	@Override
	public void mouseDragged(MouseEvent e) {
		
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		if(want){
	System.out.println(e.getX() + " X, " + e.getY() + " Y");
	try {
		Thread.sleep(1000);
	} catch (InterruptedException e1) {}
	}
	}
	
}

class VLis implements KeyListener{

	@Override
	public void keyPressed(KeyEvent e) {
		if(e.getKeyCode() == KeyEvent.VK_R){
			Game.winner = 0;
			Game.gScreen = new Screen();
			Screen.cScore = 0;
			Screen.pScore = 0;
		}
		
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
}



